package com.stox.client.ui.javafx.chartingy.model.node;

import java.util.ArrayList;
import java.util.List;

import com.stox.client.ui.javafx.chartingy.model.axis.DateAxis;
import com.stox.client.ui.javafx.chartingy.model.axis.ValueAxis;
import com.stox.client.ui.javafx.chartingy.model.plot.Data;
import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.core.util.UI;
import com.stox.shared.charting.model.Pnf;
import com.stox.shared.charting.model.Pnf.Box;
import com.stox.shared.core.model.Bar;
import com.stox.shared.core.model.Scrip;

public abstract class AbstractPnfNode<T extends AbstractPnfBox> extends AbstractPriceNode {

	protected final List<T> boxes = new ArrayList<>();

	public AbstractPnfNode(final Data<Scrip, Bar> data) {
		super(data);
		UI.style(this, CSS.pnfNode);
		final Pnf pnf = (Pnf) data.getExtraValue();
		createBoxes(data, pnf);
		getChildren().addAll(boxes);
	}
	
	@Override
	public void updateColor() {
		// TODO Auto-generated method stub
		
	}

	protected void createBoxes(final Data<Scrip, Bar> data, final Pnf pnf) {
		for (final Box box : pnf.getBoxes()) {
			boxes.add(createBox(data, box));
		}
	}

	protected abstract T createBox(final Data<Scrip, Bar> data, Box box);

	@Override
	public void layoutChartChildren() {
		final ValueAxis valueAxis = getData().getPlot().getChart().getValueAxis();
		final DateAxis dateAxis = getData().getPlot().primaryChart().getDateAxis();
		final double unitWidth = dateAxis.getUnitWidth();
		final double x = dateAxis.getDisplayPosition(getData().getIndex()) - (unitWidth / 2);
		for (final AbstractPnfBox box : boxes) {
			final double high = valueAxis.getDisplayPosition(box.getBox().getHigh());
			box.getBoxRectangle().setY(high);
			final double height = valueAxis.getDisplayPosition(box.getBox().getLow()) - high;
			box.getBoxRectangle().setHeight(height);

			box.getBoxRectangle().setX(x);
			box.getBoxRectangle().setWidth(unitWidth);
		}
	}

}
